﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DAO;
using HRProject.Code.Common;
using HRProject.Code.DTO;
using System.Collections.Generic;

namespace HRProject.Task
{
    public partial class RemoveTask : System.Web.UI.Page
    {
        private Guid taskId;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                taskId = new Guid(Request.Params["taskId"]);
            }
            catch (Exception exp)
            {
                taskId = Guid.Empty;
                Console.Write(exp.StackTrace);
            }

            if (!IsPostBack)
            {
                TaskDAO taskDAO = new TaskDAO();
                BLTaskFilter filter = new BLTaskFilter();
                filter.Id = taskId;
                filter.Name = string.Empty;
                filter.Type = new BLTaskType();
                filter.Type.Id = Guid.Empty;
                List<BLTask> tasks = taskDAO.search(filter);
                if (tasks.Count > 0)
                {
                    lblName.Text = tasks[0].Name;
                    lblTaskType.Text = tasks[0].Type.Name;
                    try
                    {
                        lblInChargeUser.Text = Membership.GetUser(tasks[0].InChargeUserId).UserName;
                    }
                    catch (Exception exp)
                    {
                        lblInChargeUser.Text = string.Empty;
                        Console.Out.WriteLine(exp.Message);
                    }
                    lblEstimateStartDate.Text = tasks[0].EstimateStartDate.ToString("dd/MM/yyyy-HH:mm");
                    lblEstimateFinishDate.Text = tasks[0].EstimateFinishDate.ToString("dd/MM/yyyy-HH:mm");
                    lblRealStartDate.Text = tasks[0].RealStartDate.ToString("dd/MM/yyyy-HH:mm");
                    lblRealFinishDate.Text = tasks[0].RealFinishDate.ToString("dd/MM/yyyy-HH:mm");
                    txaDescription.Text = tasks[0].Description;
                }
            } 
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!BLAuthentication.IsRemoveAllowed)
            {
                lblError.Text = ContentProvider.GetMessage("NotAuthorized");
                return;
            }

            BLTask filter = new BLTask();
            filter.Id = taskId;
            filter.DataSignature = 0;
            TaskDAO dao = new TaskDAO();
            dao.delete(filter);

            Util.CloseDialog(Response);
        }
    }
}
